package utils;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

public class DeviceInfoUtil {
    private static String tempDeviceId = "";

    /**
     * 添加获取机器码的方法
     */
    @NonNull
    public static String getDeviceId(Context context) {
        String deviceId = "";
        try {
            if (!TextUtils.isEmpty(tempDeviceId)) {
                return tempDeviceId;
            }
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            assert tm != null;
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
                Log.e("隐私政策", "正在获取ANDROID_ID");
                deviceId = Settings.System.getString(context.getContentResolver(), Settings.System.ANDROID_ID);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
                    deviceId = tm.getImei();
                }
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
                    deviceId = tm.getDeviceId();
                }
            } else {
                // 6.0以下自动获取到了权限
                deviceId = tm.getDeviceId();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!TextUtils.isEmpty(deviceId)) {
            tempDeviceId = deviceId;
        }
        return deviceId == null ? "" : deviceId;
    }


    /**
     * 获取当前手机系统版本号
     *
     * @return 系统版本号
     */
    public static String getSystemVersion() {
        return Build.VERSION.RELEASE;
    }

    /**
     * 手机型号
     */
    public static String getMobile() {
        return Build.MODEL;
    }


    /**
     * 获取版本号(内部识别号)
     */
    public static int getVersionCode(Context context) {
        try {
            PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return pi.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 获取版本名（Android#系统版本号#应用版本号：Android#4.1.1#1.0）
     * （"Android#" + android.os.Build.VERSION.RELEASE + "#1.0）
     */
    public static String getAppVersionName(Context context) {
        String versionName = "";
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
            if (versionName == null || versionName.length() <= 0) {
                return "";
            }
        } catch (Exception e) {
            Log.e("获取版本号错误信息", e.getMessage());
        }
        return versionName;
    }

}
